package org.agilelang.editor.tool;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.agilelang.util.Inflector;

/**
 *
 * @author DesktopPrime
 */
public final class InflextorFile {

    String output = "";
    String path_source = "";

    public InflextorFile(String path_source) {
        this.path_source = path_source;
        
        readfile();
        controller();
        controller2();
        model();
        model2();
        model3();
        //format();
        save();
    }

    public void readfile() {
        output = "";
        try {
            output = new Scanner(new File(path_source)).useDelimiter("\\Z").next();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(InflextorFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void controller() {
        Pattern pattern = Pattern.compile("(Controller\\.)(.+?)(\\.)");
        Matcher matcher = pattern.matcher(output);
        List<String> listMatches = new ArrayList<String>();
        while (matcher.find()) {
            listMatches.add(matcher.group(2));
        }
        HashSet hs = new HashSet();
        hs.addAll(listMatches);
        listMatches.clear();
        listMatches.addAll(hs);
        for (int i = 0; i < listMatches.size(); i++) {
            String new_str = Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(listMatches.get(i).toString()));
            System.out.println("pluralize:" + listMatches.get(i).toString() + " > " + new_str);
            output = output.replaceAll("Controller." + listMatches.get(i).toString() + ".", "Controller." + new_str + ".");
        }
    }

    public void controller2() {
        Pattern pattern = Pattern.compile("(controller\\s*=\\s*\")(.+?)(\")");
        Matcher matcher = pattern.matcher(output);
        List<String> listMatches = new ArrayList<String>();
        while (matcher.find()) {
            listMatches.add(matcher.group(2));
        }
        HashSet hs = new HashSet();
        hs.addAll(listMatches);
        listMatches.clear();
        listMatches.addAll(hs);
        for (int i = 0; i < listMatches.size(); i++) {
            String new_str = Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(listMatches.get(i).toString()));
            System.out.println("pluralize param :" + listMatches.get(i).toString() + " > " + new_str);
            output = output.replaceAll("controller=\"" + listMatches.get(i).toString() + "\"", "controller=\"" + new_str + "\"");
        }
    }

    public void model() {
        Pattern pattern = Pattern.compile("(Model\\.)(.+?)(\\.)");
        Matcher matcher = pattern.matcher(output);
        List<String> listMatches = new ArrayList<String>();
        while (matcher.find()) {
            listMatches.add(matcher.group(2));
        }
        HashSet hs = new HashSet();
        hs.addAll(listMatches);
        listMatches.clear();
        listMatches.addAll(hs);
        for (int i = 0; i < listMatches.size(); i++) {
            String new_str = Inflector.getInstance().capitalize(Inflector.getInstance().singularize(listMatches.get(i).toString()));
            System.out.println("singularize:" + listMatches.get(i).toString() + " > " + new_str);
            output = output.replaceAll("Model." + listMatches.get(i).toString() + ".", "Model." + new_str + ".");
        }
    }

    public void model2() {
        Pattern pattern = Pattern.compile("(model\\s*=\\s*\")(.+?)(\")");
        Matcher matcher = pattern.matcher(output);
        List<String> listMatches = new ArrayList<String>();
        while (matcher.find()) {
            listMatches.add(matcher.group(2));
        }
        HashSet hs = new HashSet();
        hs.addAll(listMatches);
        listMatches.clear();
        listMatches.addAll(hs);
        for (int i = 0; i < listMatches.size(); i++) {
            String new_str = Inflector.getInstance().capitalize(Inflector.getInstance().singularize(listMatches.get(i).toString()));
            System.out.println("singularize param:" + listMatches.get(i).toString() + " > " + new_str);
            output = output.replaceAll("model=\"" + listMatches.get(i).toString() + "\"", "model=\"" + new_str + "\"");
        }
    }
    
    public void model3() {
        Pattern pattern = Pattern.compile("(class_reference\\s*=\\s*\")(.+?)(\")");
        Matcher matcher = pattern.matcher(output);
        List<String> listMatches = new ArrayList<String>();
        while (matcher.find()) {
            listMatches.add(matcher.group(2));
        }
        HashSet hs = new HashSet();
        hs.addAll(listMatches);
        listMatches.clear();
        listMatches.addAll(hs);
        for (int i = 0; i < listMatches.size(); i++) {
            String new_str = Inflector.getInstance().capitalize(Inflector.getInstance().singularize(listMatches.get(i).toString()));
            System.out.println("singularize param:" + listMatches.get(i).toString() + " > " + new_str);
            output = output.replaceAll("class_reference=\"" + listMatches.get(i).toString() + "\"", "class_reference=\"" + new_str + "\"");
        }
    }

    public void format() {
        String[] label = {"name", "version","author","description","caption"};
        for (int x = 0; x < label.length; x++) {
            Pattern pattern = Pattern.compile("(" + label[x] + "\\s*=\\s*\")(.+?)(\")");
            Matcher matcher = pattern.matcher(output);
            List<String> listMatches = new ArrayList<String>();
            while (matcher.find()) {
                listMatches.add(matcher.group(2));
            }
            HashSet hs = new HashSet();
            hs.addAll(listMatches);
            listMatches.clear();
            listMatches.addAll(hs);
            for (int i = 0; i < listMatches.size(); i++) {
                String new_str = listMatches.get(i).toString();
                System.out.println("format param:" + listMatches.get(i).toString() + " > " + new_str);
                output = output.replaceAll(label[x] + "\\s*=\\s*\"" + listMatches.get(i).toString() + "\"", label[x] + " = \"" + new_str + "\"");
            }
        }


    }

    public void save() {
        PrintWriter out;
        try {
            out = new PrintWriter(new FileWriter(path_source + "2"));
            out.print(output);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(InflextorFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
}
